/*
 * @Author: wangjunxiao 
 * @Date: 2018-12-04 01:18:37 
 * @Last Modified by: wangjunxiao
 * @Last Modified time: 2019-06-20 23:13:43
 */
'use strict';

var conf = {
    serverHost: "http://192.168.191.1:8080"
    // serverHost: "http://localhost:8080"
}

var mm = {
    // 网路请求
    request: function (param) {
        var me = this;
        $.ajax({
            type: param.method || "get",
            url: param.url || "",
            headers:{
                'Authorization': me.getStorage($C.accessToken) || '',
            },
            dataType: param.type || "json",
            data: param.data || '',
            success: function (res) {
                // 请求成功
                if (0 === res.status) {
                    typeof param.success === "function" && param.success(res.data, res.msg);
                }
                // 需要登录,也当作错误处理吧
                else if (200 === res.status) {
                    typeof param.error === "function" && param.error(res.msg);
                }
                // 请求数据错误
                else if (100 === res.status) {
                    typeof param.error === "function" && param.error(res.msg);
                }
            },
            error: function (err) {
                typeof param.error === "function" && param.error(err.statusText);
            }
        })
    },
    // 获取服务器地址
    getServerUrl: function (path) {
        return conf.serverHost + path;
    },
    // 获取url参数
    getUrlParam: function (name) {
        // happymmall.com/product/list?keyword=xxx&page=1
        var reg = new RegExp('(^|&)' + name + "=([^&]*)(&|$)");
        // result = keyword=xxx&page=1
        var result = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;

    },
    // 渲染html页面
    renderHtml: function (htmlTemplate, data) {
        var template = Hogan.compile(htmlTemplate);
        var result = template.render(data);
        return result;
    },
    // 提示
    tips: function(msg) {
        layui.layer.msg(msg)
    },
    // 成功提示
    successTips: function (msg) {
        layer.msg(msg || "操作成功");
    },
    // 警告框 {btnName:"确定"}
    warningTips: function(msg, resolve) {
        var layer = layui.layer;
        layer.open({
            type: 1
            ,offset: "auto" //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
            ,id: 'layerWarning' //防止重复弹出
            ,content: '<div style="padding: 20px">'+ msg +'</div>'
            ,btn: "确定"
            ,btnAlign: 'c' //按钮居中
            ,shade: 0.3 //不显示遮罩
            ,yes: function(index){
                typeof resolve === "function" && resolve();
                layer.close(index);
            }
        });
    },
    // 错误提示
    errorTips: function (msg) {
        layui.layer.msg(msg || "哪里不对了~");
    },
    // 字段的验证， 支持为空判断， 手机，邮箱的判断
    validate: function (value, type) {
        var value = $.trim(value);
        // 非空验证
        if ('require' === type) {
            return !!value; // 强转成boolean类型
        }
        // 手机号验证
        if ('phone' === type) {
            return /^1\d{10}$/.test(value);
        }
        // 邮箱验证
        if ("email" === type) {
            return /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(value);
        }
    },
    // 统一登录处理, 跳转到登录页面，并记录是从哪个页面进入的登录页面
    doLogin: function () {
        window.location.href = "./login.html?redirect=" + encodeURIComponent(window.location.href);
    },
    // 跳转到首页
    goHome: function () {
        window.location.href = "./index.html";
    },
    // 跳转到指定页面
    go: function(url) {
        window.location.href= url;
    },
    // 存储键值对
    setStorage: function(key, value){
        if(arguments.length === 2){
            var v = value;
            if(typeof v == 'object'){
                v = JSON.stringify(v);
                v = 'obj-'+ v;
            }else{
                v = 'str-'+ v;
            }
            var ls = window.localStorage;
            if(ls){
                ls.setItem(key, v);
            }
        }
    },
    // 获取数据从localStorage
    getStorage : function(key){
        var ls = window.localStorage;
        if(ls){
            var v = ls.getItem(key);
            if(!v){return;}
            if(v.indexOf('obj-') === 0){
                v = v.slice(4);
                return JSON.parse(v);
            }else if(v.indexOf('str-') === 0){
                return v.slice(4);
            }
        }
    },
    rmStorage : function(key){
        var ls = window.localStorage;
        if(ls && key){
            ls.removeItem(key);
        }
    },
    clearStorage : function(){
        var ls = window.localStorage;
        if(ls){
            ls.clear();
        }
    }
};